package cn.youyeku.zsxh.ui.practice;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import java.util.ArrayList;
import java.util.List;

import cn.youyeku.zsxh.Utils.LogUtils;


/**
 * Created by youye on 17-4-3.
 */

public abstract class SingleAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected List<T> mDatas = new ArrayList<>();
    protected int mItemId;

    protected int lastPos = -1;




    protected SingleAdapter(int mItemLayout) {
        this.mItemId = mItemLayout;

    }

    @Override
    public boolean onFailedToRecycleView(RecyclerView.ViewHolder holder) {

        LogUtils.w("onFailedToRecycleView");
        return super.onFailedToRecycleView(holder);
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        LogUtils.w("onViewAttachedToWindow");
        super.onViewAttachedToWindow(holder);


    }

    @Override
    public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
        LogUtils.w("onViewDetachedFromWindow");
        super.onViewDetachedFromWindow(holder);
    }

    @Override
    public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        LogUtils.w("registerAdapterDataObserver ");
        super.registerAdapterDataObserver(observer);
    }

    @Override
    public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        LogUtils.w("unregisterAdapterDataObserver ");
        super.unregisterAdapterDataObserver(observer);
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        LogUtils.w("onDetachedFromRecyclerView");
        super.onDetachedFromRecyclerView(recyclerView);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {


        super.onAttachedToRecyclerView(recyclerView);


    }

    @Override
    public void onViewRecycled(RecyclerView.ViewHolder holder) {

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new ListHolder(LayoutInflater.from(parent.getContext()).inflate(mItemId, parent, false));


//        return new ListHolder(LayoutInflater.from(parent.getContext()).inflate(mItemId, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ListHolder listHolder = (ListHolder) holder;
        View item = listHolder.itemView;
        onBindData(listHolder, item, mDatas.get(position), position);


    }


    protected abstract void onBindData(ListHolder listHolder, View item, T data, int pos);


    public void add(List<T> data) {
        mDatas = data;
        if (mDatas.size() > 0) {
            notifyDataSetChanged();
        } else {
            LogUtils.w("没有数据");
        }
    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //进行控件绑定
    @SuppressWarnings("unchecked")
    protected class ListHolder extends RecyclerView.ViewHolder {


        public ListHolder(View itemView) {
            super(itemView);
        }

        public <T extends View> T getChildView(int id) {
            return (T) itemView.findViewById(id);
        }


    }

}
